package com.zhenyi.youqu.miniapp;

import java.util.List;

import com.cjw.model.DBMap;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.SqlPara;
import com.youqu.common.model.YqGroup;
import com.youqu.common.model.YqOrderInfo;
import com.youqu.common.model.YqProduct;
import com.youqu.common.model.YqUser;
import com.youqu.common.model.YqUserOrderGroup;
import com.zhenyi.youqu.core.YQMiniAppController;

/**
 * 团购
 * 
 * @author cen
 *
 */
public class GroupController extends YQMiniAppController {

	/**
	 * 团购详情
	 */
	@Override
	public void info() {
		super.info();
	}

	/**
	 * 我这个团的人
	 */
	public void joinList() {
		Long productId = getParaToLong("productId");
		Long groupId = getParaToLong("groupId");
		if (productId == null) {
			renderError(404);
			return;
		}

		YqProduct product = YqProduct.dao.findById(productId);
		if (product == null) {
			renderError(404);
			return;
		}
		DBMap map = new DBMap();
		map.put("product", product);

		YqUser user = getUser();
		Long userId = user.getId();

		YqGroup group = null;
		if (groupId != null) {
			group = YqGroup.dao.findById(groupId);
			if (group == null) {
				map.put("isGroupUp", 0);
				map.put("msg", "尚未发起拼团");
				renderResult(map);
				// renderFailure(500, "尚未发起拼团");
				return;
			}
		} else {
			Kv orderInfoKv = Kv.create();
			orderInfoKv.set("userId", userId);
			orderInfoKv.set("productId", productId);
			SqlPara orderInfoSqlPara = db.getSqlPara("YQFindItemGroup", orderInfoKv);
			YqOrderInfo orderInfo = YqOrderInfo.dao.findFirst(orderInfoSqlPara);
			if (orderInfo == null) {
				map.put("isGroupUp", 0);
				map.put("msg", "尚未发起拼团");
				renderResult(map);
				// renderFailure(500, "尚未发起拼团");
				return;
			}
			// DBMap maps = new DBMap();
			// maps.put("userId", userId);
			// maps.put("productId", productId);
			// Long groupId = getParaToLong("groupId");
			// YqGroup group = YqGroup.dao.findBy(userId, productId);
			group = YqGroup.dao.findById(orderInfo.getGroupId());
			if (group == null) {
				map.put("isGroupUp", 0);
				map.put("msg", "尚未发起拼团");
				renderResult(map);
				// renderFailure(500, "尚未发起拼团");
				return;
			}
			// group = new YqGroup();
			// group.setUserId(userId);
			// group.setProductId(productId);
			// group.save();
			// }
			groupId = group.getId();
		}
		group.put("expireTimestamp", group.getExpire().getTime()); // 拼团页面倒计时过期时间戳
		group.updateGroupUserCount();
		List<YqUser> users = group.getJoinedUser();

		map.put("isGroupUp", 1);
		map.put("group", group);
		map.put("user", users);
		renderSuccess(map);
	}

	/**
	 * 参与团购
	 */
	public void join() {
		Long groupId = getParaToLong("groupId");
		if (groupId == null) {
			renderError(404);
			return;
		}
		YqGroup group = YqGroup.dao.findById(groupId);
		if (group == null) {
			renderError(404);
			return;
		}
		group.updateGroupUserCount();
		if (group.getIsDone().equals(1)) {
			renderMsg("已满员");
			return;
		}
		YqUser user = getUser();
		Long userId = user.getId();
		YqUserOrderGroup userOrderGroup = YqUserOrderGroup.dao.findById(groupId, userId);
		if (userOrderGroup == null) {
			userOrderGroup = new YqUserOrderGroup();
			userOrderGroup.setUserId(userId);
			userOrderGroup.setGroupId(groupId);
			userOrderGroup.save();
		}
		renderSuccess();
	}

}
